package dev.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @program: JoyDev
 * @description: web 层相关方法工具类
 * @author: MR.C
 * @create: 2018-02-08 15:16
 **/
public class WebUtils {

    /**
     * @Description: 获取请求参数转换成Map对象
     * @Param: [request]
     * @return: java.util.Map<java.lang.String   ,   java.lang.Object>
     * @Author: Mr.Chen
     * @Date: 2018/2/8 下午3:17
     */
    public static Map<String, Object> getParamMap(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Map.Entry<String, String[]>> entrySet = parameterMap.entrySet();
        Map<String, Object> params = new HashMap<>();
        for (Map.Entry<String, String[]> entry : entrySet) {
            params.put(entry.getKey(), entry.getValue()[0]);
        }
        return params;
    }

    /**
     * @Description: 获取请求参数转化为model对象
     * @Param: [request, clazz]
     * @return: T
     * @Author: Mr.Chen
     * @Date: 2018/2/8 下午3:18
     */
    public static <T> T param2Model(HttpServletRequest request, Class<T> clazz) {
        Map<String, Object> params = getParamMap(request);
        return JSONObject.toJavaObject((JSON) params, clazz);
    }


    public static String getCurrUser() {
        return "System";
    }
}
